/*
 *
 * Created on: 14.07.2012
 *
 * DHConfig.cpp
 * This file is part of KOBUS
 * 
 * Copyright (C) 2012 - Ivan Penkin, MIPT
 * grek.penkin@gmail.com
 *
 * KOBUS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * KOBUS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with KOBUS; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, 
 * Boston, MA  02110-1301  USA
 *  
 */


#include <QtNetwork>
#include <DHConfig.h>

#include <errno.h>


using namespace kobus::dhcp;

INIT_CONFIG_KEY(DHConfig::Keys::daemon::user);
INIT_CONFIG_KEY(DHConfig::Keys::daemon::group);
INIT_CONFIG_KEY(DHConfig::Keys::connection::id);
INIT_CONFIG_KEY(DHConfig::Keys::connection::coreAddr);
INIT_CONFIG_KEY(DHConfig::Keys::connection::corePort);
INIT_CONFIG_KEY(DHConfig::Keys::connection::coreKey);
INIT_CONFIG_KEY(DHConfig::Keys::dhcpLog::logPath);
INIT_CONFIG_KEY(DHConfig::Keys::dhcpLog::allowedIfaces);
INIT_CONFIG_KEY(DHConfig::Keys::ackPraser::ackRegex);
INIT_CONFIG_KEY(DHConfig::Keys::ackPraser::ifaceGrp);
INIT_CONFIG_KEY(DHConfig::Keys::ackPraser::vlanGrp);
INIT_CONFIG_KEY(DHConfig::Keys::ackPraser::macGrp);
INIT_CONFIG_KEY(DHConfig::Keys::ackPraser::ipGrp);
INIT_CONFIG_KEY(DHConfig::Keys::ackPraser::remoteIdGrp);
INIT_CONFIG_KEY(DHConfig::Keys::ackPraser::circuitIdGrp);
INIT_CONFIG_KEY(DHConfig::Keys::releaseParser::releaseRegex);
INIT_CONFIG_KEY(DHConfig::Keys::releaseParser::macGrp);
INIT_CONFIG_KEY(DHConfig::Keys::releaseParser::ipGrp);

DHConfig::DHConfig(QObject * parent)
    :Config(parent) {

}


DHConfig::DHConfig(const QString & path, QObject * parent)
    :Config(path, parent) {

}


#define WRONG_VALUE(Key) \
  log.error("Wrong value of '"+Key.sectionName()+"."+Key.getName()+"'.");\
  return false;

#define CHK_INT_BETWEN(Key,From,To) \
  if (getInt(Key) < From \
        || getInt(Key) > To) { \
    WRONG_VALUE(Key); \
  }

#define CHK_STR_ISNOT_EMPTY(Key) \
  if (getString(Key).isEmpty()) { \
    WRONG_VALUE(Key); \
  }

bool DHConfig::check() const {

  Log log;

  try {

    /* DHConfig::Keys::connection:: */
    if (getInt(DHConfig::Keys::connection::id) == 0) {
      WRONG_VALUE(DHConfig::Keys::connection::id);
    }

    if (QHostAddress(
            getString(DHConfig::Keys::connection::coreAddr)).isNull()) {
      WRONG_VALUE(DHConfig::Keys::connection::coreAddr);
    }

    CHK_INT_BETWEN(DHConfig::Keys::connection::corePort, 0, 65636);
    CHK_STR_ISNOT_EMPTY(DHConfig::Keys::connection::coreKey);

    /* DHConfig::Keys::connection:: */


    /* DHConfig::Keys::dhcpLog:: */
    CHK_STR_ISNOT_EMPTY(Keys::dhcpLog::logPath);
    CHK_STR_ISNOT_EMPTY(Keys::dhcpLog::allowedIfaces);
    /* DHConfig::Keys::dhcpLog:: */


    /* DHConfig::Keys::ackPraser:: */
    CHK_STR_ISNOT_EMPTY(DHConfig::Keys::ackPraser::ackRegex);

    /* non-optional */
    CHK_INT_BETWEN(Keys::ackPraser::ifaceGrp,
        MIN_NONOPTIONAL_GROUP_NUM, MAX_GROUP_NUM);
    CHK_INT_BETWEN(Keys::ackPraser::macGrp,
        MIN_NONOPTIONAL_GROUP_NUM, MAX_GROUP_NUM);
    CHK_INT_BETWEN(Keys::ackPraser::ipGrp,
        MIN_NONOPTIONAL_GROUP_NUM, MAX_GROUP_NUM);
    /* non-optional */

    /* optional */
    CHK_INT_BETWEN(Keys::ackPraser::vlanGrp,
        MIN_OPTIONAL_GROUP_NUM, MAX_GROUP_NUM);
    CHK_INT_BETWEN(Keys::ackPraser::circuitIdGrp,
        MIN_OPTIONAL_GROUP_NUM, MAX_GROUP_NUM);
    CHK_INT_BETWEN(Keys::ackPraser::remoteIdGrp,
        MIN_OPTIONAL_GROUP_NUM, MAX_GROUP_NUM);
    /* optional */

    /* DHConfig::Keys::ackPraser:: */

    /* DHConfig::Keys::releasePraser:: */
    CHK_STR_ISNOT_EMPTY(DHConfig::Keys::releaseParser::releaseRegex);

    // TODO: check optionality of mac & ip groups
    CHK_INT_BETWEN(Keys::releaseParser::macGrp,
        MIN_NONOPTIONAL_GROUP_NUM, MAX_GROUP_NUM);
    CHK_INT_BETWEN(Keys::releaseParser::ipGrp,
        MIN_NONOPTIONAL_GROUP_NUM, MAX_GROUP_NUM);
    /* DHConfig::Keys::releasePraser:: */

  } catch (Exception e) {
    return false;
  }

  return true;
}

